package com.crt.uav.api.v1;

import com.crt.uav.annotation.UavOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.Map;

@RestController
@RequestMapping("/api/v1")
@Tag(name = "无人机信息查询接口", description = "提供给无人机信息查询的接口， 包活电量查询、实时状态查询、速度查询、里程查询、运行时长查询")
public class UAVInfoEndpoint {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${uav.system.apiurl}")
    private String uavApiUrl;

    @UavOperation(summary = "无人机速度查询", description = "获取指定无人机最新一条无人机速度信息",
            parameters = {
                    @Parameter(name = "uav_sn", description = "无人机序列号", required = true)
            })
    @GetMapping("/getNewHud")
    public String getNewHud(@RequestParam String uav_sn) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(uavApiUrl + "/getNewHud")
                .queryParam("uav_sn", uav_sn);
        ResponseEntity<String> result = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, null, String.class);
        return result.getBody();
    }

    @UavOperation(summary = "无人机最新里程、运行时间查询", description = "获取指定无人机最新一条无人机里程、运行时长信息",
            parameters = {
                    @Parameter(name = "uav_sn", description = "无人机序列号", required = true)
            })
    @GetMapping("/getNewNed")
    public String getNewNed(@RequestParam String uav_sn) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(uavApiUrl + "/getNewNed")
                .queryParam("uav_sn", uav_sn);
        ResponseEntity<String> result = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, null, String.class);
        return result.getBody();
    }

    @UavOperation(summary = "无人机急停", description = "控制无人机急停",
            parameters = {
                    @Parameter(name = "uav_sn", description = "无人机序列号", required = true)
            })
    @PutMapping("/uavScram")
    public String uavScram(@RequestParam String uav_sn) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(uavApiUrl + "/uavScram")
                .queryParam("uav_sn", uav_sn);
        ResponseEntity<String> result = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, null, String.class);
        return result.getBody();
    }

    @UavOperation(summary = "无人机返航", description = "控制无人机返航",
            parameters = {
                    @Parameter(name = "uav_sn", description = "无人机序列号", required = true)
            })
    @PutMapping("/uavReturn")
    public String uavReturn(@RequestParam String uav_sn) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(uavApiUrl + "/uavReturn")
                .queryParam("uav_sn", uav_sn);
        ResponseEntity<String> result = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, null, String.class);
        return result.getBody();
    }

    /**
     * 获取无人机详情(状态) post 请求
     */
//    @UavOperation(summary = "无人机详情", description = "获取无人机详情(状态)",
//            parameters = {
//                    @Parameter(name = "uav_sn", description = "无人机序列号", required = true)
//            })
    @Operation(
            summary = "无人机详情",
            description = "获取无人机详情(状态)",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "无人机序列号",
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(
                                    title = "无人机序列号",
                                    description = "无人机序列号",
                                    type = "object",
                                    example = "{\"uav_sn\": \"1\"}"
                            )
                    )
            ),
            responses = {
                    @ApiResponse(
                            description = "获取无人机详情(状态)",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            title = "无人机详情(状态)",
                                            description = "无人机详情(状态)",
                                            type = "object"
                                    )
                            )
                   )
            }
    )
    @PostMapping("/getUavDetail")
    public String getUavDetail(@RequestBody Map<String, Object> params) {
        RequestEntity<Map<String, Object>> requestEntity = RequestEntity.post(uavApiUrl + "/getUavDetail").body(params);
        ResponseEntity<String> result = restTemplate.exchange( requestEntity, String.class);
        return result.getBody();
    }

    /**
     * 获取指定无人机最新一条无人机电池信息
     * get 请求
     */
    @UavOperation(summary = "获取指定无人机最新一条无人机电池信息", description = "获取的是指定无人机的最新一条无人机电池信息",
            parameters = {
                    @Parameter(name = "uav_sn", description = "无人机序列号", required = true)
            })
    @GetMapping("/getNewBattery")
    public String getNewBattery(@RequestParam String uav_sn) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(uavApiUrl + "/getNewBattery")
                .queryParam("uav_sn", uav_sn);
        ResponseEntity<String> result = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, null, String.class);
        return result.getBody();
    }

    /**
     * 取消无人机急停
     * get 请求
     */
    @UavOperation(summary = "取消无人机急停", description = "控制无人机取消急停",
            parameters = {
                    @Parameter(name = "uav_sn", description = "无人机序列号", required = true)
            })
    @GetMapping("/uavCancelScram")
    public String uavCancelScram(@RequestParam String uav_sn) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(uavApiUrl + "/uavCancelScram")
                .queryParam("uav_sn", uav_sn);
        ResponseEntity<String> result = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, null, String.class);
        return result.getBody();
    }


}
